package com.siao.starling.display
{
	import com.siao.display.Range;
	
	import flash.geom.Point;
	
	import starling.display.DisplayObject;
	import starling.display.DisplayObjectContainer;
	
	/**
	 * 测量容器的实际显示范围
	 * @param container 容器对象
	 * @param ignoreH 水平忽略列表，测量水平数值时将忽略此列表中的所有显示对象
	 * @param ignoreV 垂直忽略列表，测量垂直数值时将忽略此列表中的所有显示对象
	 * @return
	 */
	public function measure(container:DisplayObjectContainer, ignoreH:Array=null, ignoreV:Array=null):Range
	{
		//如果父级是其他容器则返回所有子级的实际显示范围
		var rectX:Number;
		var rectY:Number;
		var rectW:Number;
		var rectH:Number;
		
		for (var i:int = 0; i < container.numChildren; i++)
		{
			var child:DisplayObject = container.getChildAt(i);
			
			if (!child.visible)
			{
				continue;
			}
			
			if (ignoreH == null || ignoreH.indexOf(child) < 0)
			{
				rectX = isNaN(rectX) ? child.x : Math.min(rectX, child.x);
				rectW = isNaN(rectW) ? child.width : Math.max(rectW, child.x + child.width - rectX);
			}
			
			if (ignoreV == null || ignoreV.indexOf(child) < 0)
			{
				rectY = isNaN(rectY) ? child.y : Math.min(rectY, child.y);
				rectH = isNaN(rectH) ? child.height : Math.max(rectH, child.y + child.height - rectY);
			}
		}
		
		var range:Range = new Range();
		range.left = isNaN(rectX) ? 0 : rectX;
		range.top = isNaN(rectY) ? 0 : rectY;
		range.width = (isNaN(rectW)) ? (container.width <= 0 && this.stage != null) ? this.stage.stageWidth - container.localToGlobal(new Point(0, 0)).x : container.width : rectW;
		range.height = (isNaN(rectH)) ? (container.height <= 0 && this.stage != null) ? this.stage.stageHeight - container.localToGlobal(new Point(0, 0)).y : container.height : rectH;
		range.right = container.width - range.left - range.width;
		range.bottom = container.height - range.top - range.height;
		
		return range;
	}

}
